package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import com.mars.framework.exception.ServiceException;
import com.mars.module.admin.response.SysDeptResponse;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysDeptRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysDeptMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysDept;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysDeptService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 部门业务层处理
 *
 * @author mars
 * @date 2024-04-02
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysDeptServiceImpl implements ISysDeptService {

    private final SysDeptMapper baseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysDept add(SysDeptRequest request) {
        SysDept entity = SysDept.builder().build();
        BeanUtils.copyProperties(request, entity);
        entity.setParentId(request.getParentId() == null ? 0L : request.getParentId());
        Long id = ContextUserInfoThreadHolder.get().getId();
        entity.setCreateById(id);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysDeptRequest request) {
        SysDept entity = SysDept.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysDeptResponse get(Long id) {
        SysDept productClass = baseMapper.selectById(id);
        if (Objects.isNull(productClass)) {
            throw new ServiceException("数据不存在");
        }
        SysDeptResponse classResponse = new SysDeptResponse();
        BeanUtils.copyProperties(productClass, classResponse);
        return classResponse;
    }

    @Override
    public SysDept getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<SysDept> pageList(SysDeptRequest request) {
        Page<SysDept> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysDept> query = this.buildWrapper(request);
        IPage<SysDept> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysDept> list(SysDeptRequest request) {
        LambdaQueryWrapper<SysDept> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    @Override
    public List<SysDeptResponse> tree(SysDeptRequest queryDto) {
        List<SysDept> list = baseMapper.selectByCondition(queryDto);
        List<SysDeptResponse> voList = new ArrayList<>();
        for (SysDept sysMenu : list) {
            SysDeptResponse sysMenuVo = new SysDeptResponse();
            if (sysMenu.getParentId().equals(0L)) {
                BeanUtils.copyProperties(sysMenu, sysMenuVo);
                sysMenuVo.setChildren(getChildren(list, sysMenu.getId()));
                voList.add(sysMenuVo);
            }
        }
        return voList;
    }

    @Override
    public List<SysDeptResponse> getChildren(List<SysDept> list, Long id) {
        List<SysDeptResponse> voList = new ArrayList<>();
        for (SysDept sysMenu : list) {
            SysDeptResponse sysMenuVo = new SysDeptResponse();
            if (sysMenu.getParentId() != null && sysMenu.getParentId().equals(id)) {
                BeanUtils.copyProperties(sysMenu, sysMenuVo);
                sysMenuVo.setChildren(getChildren(list, sysMenu.getId()));
                voList.add(sysMenuVo);
            }
        }
        return voList;
    }

    private LambdaQueryWrapper<SysDept> buildWrapper(SysDeptRequest param) {
        LambdaQueryWrapper<SysDept> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getDeptName())) {
            query.like(SysDept::getDeptName, param.getDeptName());
        }
        if (param.getParentId() != null) {
            query.like(SysDept::getParentId, param.getParentId());
        }
        return query;
    }

}
